package cn.edu.scau.cmi.chenyiqiu.spring.client;

import java.util.HashSet;
import java.util.List;
import java.util.Scanner;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.springframework.context.ApplicationContext;

import cn.edu.scau.cmi.chenyiqiu.spring.domain.Student;
import cn.edu.scau.cmi.chenyiqiu.spring.domain.Teacher;
import cn.edu.scau.cmi.chenyiqiu.spring.hibernate.dao.StudentDao;
import cn.edu.scau.cmi.chenyiqiu.spring.hibernate.dao.TeacherDao;
import cn.edu.scau.cmi.chenyiqiu.spring.util.ApplicationContextUtil;

public class Client {
	private static ApplicationContext application=ApplicationContextUtil.getApplicationContext();
	private static StudentDao studentDao=(StudentDao)application.getBean("StudentDao");
	private static TeacherDao teacherDao=(TeacherDao)application.getBean("TeacherDao");
	private static Scanner scanner=new Scanner(System.in);
	
	public static void main(String[] args) {
		while(true) {
			System.out.println("功能1：添加学生");
			System.out.println("功能2：修改学生");
			System.out.println("功能3：删除学生");
			System.out.println("功能4：添加教师");
			System.out.println("功能5：修改教师");
			System.out.println("功能6：删除教师");
			System.out.println("功能7：学生选择指导教师");
			System.out.println("功能8：学生修改指导教师");
			System.out.println("功能9：教师选择被指导的学生");
			System.out.println("功能10：教师修改被指导的学生");
			System.out.print("请选择你需要的功能:");
			int flag=scanner.nextInt();
			scanner.nextLine();
			switch (flag) {
			case 1:
				addStudent();
				break;
	        case 2:
	        	updateStudent();
				break;
	        case 3:
	        	deleteStudent();
		        break; 
	        case 4:
	        	addTeacher();
		        break; 
	        case 5:
	        	updateTeacher();
		        break; 
	        case 6:
	        	deleteTeacher();
		        break; 
	        case 7:
	        	stuChooseTea();
		        break; 
	        case 8:
	        	stuUpdateTea();
		        break; 
	        case 9:
	        	teaChooseStu();
		        break; 
	        case 10:
	        	teaUpdateStu();
		        break;
			default:
				return;
			}
		}
		
	}


	private static void teaUpdateStu() {
		System.out.println("请输入老师的id:");
		Long tea_id=scanner.nextLong();
		scanner.nextLine();
		Teacher teacher=teacherDao.findById(tea_id);
		System.out.println("请输入要修改学生的id:");
		Long stu_id=scanner.nextLong();
		scanner.nextLine();
		Student student=studentDao.findById(stu_id);
		student.setTeacher(teacher);
		studentDao.update(student);
	}





	private static void teaChooseStu() {
		System.out.println("请输入老师的id:");
		Long tea_id=scanner.nextLong();
		scanner.nextLine();
		Teacher teacher=teacherDao.findById(tea_id);
		System.out.println("请输入要选择学生的id:");
		Long stu_id=scanner.nextLong();
		scanner.nextLine();
		Student student=studentDao.findById(stu_id);
		student.setTeacher(teacher);
		studentDao.update(student);
	}





	private static void stuUpdateTea() {
		System.out.println("请输入学生的id:");
		Long stu_id=scanner.nextLong();
		scanner.nextLine();
		Student student=studentDao.findById(stu_id);
		System.out.println("请输入要修改老师的id:");
		Long tea_id=scanner.nextLong();
		scanner.nextLine();
		Teacher teacher=teacherDao.findById(tea_id);
		student.setTeacher(teacher);
		studentDao.update(student);
	}





	private static void stuChooseTea() {
		System.out.println("请输入学生的id:");
		Long stu_id=scanner.nextLong();
		scanner.nextLine();
		Student student=studentDao.findById(stu_id);
		System.out.println("请输入要选择老师的id:");
		Long tea_id=scanner.nextLong();
		scanner.nextLine();
		Teacher teacher=teacherDao.findById(tea_id);
		student.setTeacher(teacher);
		studentDao.update(student);
		
	}





	private static void deleteTeacher() {
		System.out.print("请输入要删除教师的id:");
		Long id=scanner.nextLong();
		Teacher teacher=teacherDao.findById(id);
		scanner.nextLine();
		teacherDao.delete(teacher);
	}

	private static void updateTeacher() {
		System.out.print("请输入要修改教师的id:");
		Long id=scanner.nextLong();
		Teacher teacher=teacherDao.findById(id);
		scanner.nextLine();
		System.out.print("请输入要修改后教师的名字:");
		String name=scanner.nextLine();
		teacher.setName(name);
		teacherDao.update(teacher);
	}

	private static void addTeacher() {
		System.out.print("请输入教师的名字:");
		String name=scanner.nextLine();
		Teacher teacher=new Teacher();
		teacher.setName(name);
		teacherDao.save(teacher);
	}

	private static void deleteStudent() {
		System.out.print("请输入要删除学生的id:");
		Long id=scanner.nextLong();
		Student student=studentDao.findById(id);
		scanner.nextLine();
		studentDao.delete(student);
	}

	private static void updateStudent() {
		System.out.print("请输入要修改学生的id:");
		Long id=scanner.nextLong();
		Student student=studentDao.findById(id);
		scanner.nextLine();
		System.out.print("请输入要修改后学生的名字:");
		String name=scanner.nextLine();
		student.setName(name);
		studentDao.update(student);
	}


	private static void addStudent() {
		System.out.print("请输入学生的名字:");
		String name=scanner.nextLine();
		Student student=new Student();
		student.setName(name);
		studentDao.save(student);
	}
	
	
}
